package genosAgape.util;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dialog;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;

public class DialogoOcupado extends JDialog {
	private boolean duracaoIndeterminada = true;
	private JLabel mensagem;
	private JProgressBar progress;

	public DialogoOcupado(String msg) {
		setSize(250, 40);
		setLocationRelativeTo(null);
		//setDefaultCloseOperation(HIDE_ON_CLOSE);
		buildComponente(msg);
		setUndecorated(true);
		pack();
		setModal(true);
	}

	private void buildComponente(String msg) {
		JPanel content = new JPanel();
		progress = new JProgressBar();
		progress.setIndeterminate(duracaoIndeterminada);
		mensagem = new JLabel(msg);
		content.setLayout(new BorderLayout());
		content.add(progress, "Center");
		content.add(mensagem, "South");
		content.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createLineBorder(Color.black),
				BorderFactory.createEmptyBorder(6, 6, 6, 6)));
		setContentPane(content);
	}

	public void atualiza(final String novaMsg) {
		try {
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					mensagem.setText(novaMsg);
					if (!duracaoIndeterminada)
						progress.setValue(progress.getValue() + 1);
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void fecharDialogo() {
		setVisible(false);
		dispose();
	}
	
	public void abrirDialogo() {
		Thread t = new Thread(new Runnable() {
		      public void run() {		
		    	  setVisible(true);
		      }
		    });
		t.start();
	}	
	
	public boolean isDuracaoIndeterminada() {
		return duracaoIndeterminada;
	}
	
	public String getMensagem() {
		return mensagem.getText();
	}
}
